﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Aspfm.Dal;
using System.Collections;
using Aspfm.Main;

namespace Aspfm.DataBase
{
    public partial class Form1 : Form
    {
        public static DataSourceList datalist = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node != null)
            {
                string oldkey = node.Text;
                foreach (TreeNode n in treeView1.Nodes)
                {
                    if (n != node && n.Text.ToLower() == textBox1.Text.ToLower().Trim())
                    {
                        MessageBox.Show("连接关键字重复不允许保存");
                        return;
                    }
                }

                DataSource data = GetSelData();
                data = UCConn(data, true);
                node.Text = data.Key = textBox1.Text.Trim();
                string errmsg="";
                if (DbTool.TestConn(ref errmsg, data))
                {
                    node.Tag = data;

                    foreach (DataSource ds in datalist.dataList)
                    {
                        if (ds.Key.ToLower() == oldkey.ToLower())
                        {
                            datalist.dataList.Remove(ds);
                            break;
                        }
                    }
                    datalist.dataList.Add(data);
                    ConfigTool.SaveConn(datalist);
                }
                else
                {
                    MessageBox.Show(errmsg);
                }
            }
        }

        private DataSource UCConn(DataSource data, bool isGetValue)
        {
            if (isGetValue)
            {
                switch (comboBox1.SelectedIndex)
                {
                    case 0:
                        data = ucOracle1.GetData(data.Key);
                        break;
                    case 1:
                        data = ucMSSql1.GetData(data.Key);
                        break;
                    case 2:
                        data = new DataSource();
                        data.datatype = DataSourceType.SQLite;
                        data.ConnStr = textBox2.Text;
                        break;
                }
            }
            else
            {
                if (data.datatype == DataSourceType.Oracle)
                {
                    ucOracle1.Visible = true;
                    data.datatype = DataSourceType.Oracle;


                    comboBox1.SelectedIndex = 0;
                    ucOracle1.SetData(data);
                }
                else if (data.datatype == DataSourceType.MSSql)
                {
                    comboBox1.SelectedIndex = 1;
                    ucMSSql1.SetData(data);
                }
                else if (data.datatype == DataSourceType.SQLite)
                {
                    comboBox1.SelectedIndex =2;
                    textBox2.Text = data.ConnStr;
                }
            }
            return data;
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.Nodes.Add("NewData1");
            treeView1.SelectedNode = node;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null && treeView1.SelectedNode.Text.ToLower()!="default")
            {
                DialogResult dia = MessageBox.Show("是否确定删除？", "删除提示", MessageBoxButtons.YesNo);
                if (dia == DialogResult.Yes)
                {
                    if (treeView1.SelectedNode.Tag != null && treeView1.SelectedNode.Tag is DataSource &&
                        datalist != null && datalist.dataList != null)
                    {
                        datalist.dataList.Remove((DataSource)treeView1.SelectedNode.Tag);
                        ConfigTool.SaveConn(datalist);
                    }
                    treeView1.SelectedNode.Remove();
                }
            }
        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            TreeNode node = treeView1.GetNodeAt(e.X, e.Y);
            if (node != null)
            {
                treeView1.SelectedNode = node;
            }
            else
            {
                treeView1.SelectedNode = treeView1.Nodes[0];
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node=e.Node;
            if (node != null)
            {
                DataSource data = GetSelData(node);
                textBox1.ReadOnly = false;
                if (node.Text.ToLower() == "default")
                {
                    textBox1.ReadOnly = true;
                }
                data = UCConn(data, false);
                textBox1.Text = data.Key = node.Text;
                node.Tag = data;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            datalist = ConfigTool.ReadConn();
            if (datalist == null)
            {
                datalist = new DataSourceList();
            }
            if (datalist.dataList == null)
            {
                datalist.dataList = new List<DataSource>();
            }
            foreach (DataSource data in datalist.dataList)
            {
                if (data.Key.ToLower() == "default")
                {
                    treeView1.Nodes[0].Remove();
                }
                TreeNode node = treeView1.Nodes.Add(data.Key);
                node.Tag = data;
            }

            treeView1.SelectedNode = treeView1.Nodes[0];

            ucParams1.Init();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ucMSSql1.Visible = false;
            ucOracle1.Visible = false;
            textBox2.Visible=false;
            textBox2.Text="";
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    ucOracle1.Clear();
                    ucOracle1.Visible = true;
                    break;
                case 1:
                    ucMSSql1.Clear();
                    ucMSSql1.Visible = true;
                    break;
                default :
                    textBox2.Visible=true;
                    break;
            }
        }

        private DataSource GetSelData(TreeNode node=null)
        {
            DataSource data = null;
            if (node == null)
            {
                node = treeView1.SelectedNode;
            }
            if (node != null && node.Tag is DataSource)
            {
                data = (DataSource)node.Tag;
            }
            else
            {
                data = new DataSource();
            }
            return data;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            TreeNode node = treeView1.SelectedNode;
            if (node != null)
            {
                DataSource data = GetSelData();
                data = UCConn(data, true);
                string errmsg="";
                if (!DbTool.TestConn(ref errmsg, data))
                {
                    MessageBox.Show(errmsg);
                }
                else
                {
                    MessageBox.Show("连接成功!");
                }

                //Aspfm.Dal.IDbHelper idb = Aspfm.Dal.DbFactory.GetDB();
                //idb.TransactionBegin();
                //try
                //{
                //    idb.ExecSql("update bj_00_00_bjinfo set remark='33' where bjno='660500WwmDwhniIcSD'", null);
                //    List<IDataParameter> dbparameter = new List<IDataParameter>();
                //    dbparameter.Add(idb.ParamInit("MYPARAM", DateTime.Now.ToString()));
                //    idb.ExecProc("MYPROC".ToUpper(), dbparameter, false, true);
                //    DataSet ds = idb.QuerySql("Select remark from bj_00_00_bjinfo where bjno='660500WwmDwhniIcSD'");
                //    Hashtable ht = new Hashtable();
                //    ht.Add("bjno", "1234");
                //    ht.Add("remark", "333");
                //    idb.SaveData("bj_00_00_bjinfo", ht, "bjno", true);
                //    idb.TransactionClose(true);
                //}
                //catch (Exception ex)
                //{
                //    idb.TransactionClose(false);
                //}
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab.Text.Contains("FTP文件"))
            {
                ucFtp1.Init();
            }
            else if (tabControl1.SelectedTab.Text.Contains("分页语句设置"))
            {
                ucSqlPage1.Init();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataSource data = GetSelData();
            if (data != null && !string.IsNullOrEmpty(data.Key))
            {
                string errmsg = "";
                if (DbTool.TestConn(ref errmsg, data))
                {
                    FrmLock flock = new FrmLock(data.Key);
                    flock.ShowDialog();
                }
                else
                {
                    MessageBox.Show(errmsg);
                }
            }
            else
            {
                MessageBox.Show("先选择数据库。");
            }
        }
    }
}
